---__init__
if _G["__init__"] then
    local arg = ...
    return {
        thread = 8,
        enable_stdout = (arg[3] ~= "hide"),
        logfile = string.format("log/entry-%s-%s.log", arg[1], os.date("%Y-%m-%d-%H-%M-%S")),
        loglevel = arg[4] or "DEBUG",
        path = table.concat({
            "./?.lua",
            "./?/init.lua",
            -- "game/?.lua",
            "moon/lualib/?.lua",
            "moon/service/?.lua",
            -- Append your lua module search path
            "./luaext/?.lua",
        },";")
    }
end

local moon = require("moon")
local json = require("json")
local http_server = require("moon.http.server")
local share = require 'common.Share'

local arg = moon.args()

local http_server_conf
local res = json.decode(io.readfile(arg[2]))
for _, v in ipairs(res) do
    if v.node == tonumber(arg[1]) then
        http_server_conf = v
    end
end
print('------  ' .. http_server_conf.host)

-- httpserver.content_max_len = 8192

http_server.error = function(fd, err)
    print("http server fd",fd," disconnected:",  err)
end

http_server.on("/entryHandler.launcher", function(request, response)
    local query = request:parse_query()
    -- print('======  ' .. query.channel .. ' ' .. query.version)
    
    local hotfix = {
        openHotfix = true,
        version = 22,
        minVersion = 0,
        maxVersion = 9999,
        url = 'http://' .. http_server_conf.host .. '/hotfix'
    }

    response.status_code = 200
    response:write_header("Content-Type","application/json")
    response:write(json.encode(hotfix))
end)

http_server.on('/hotfix', function (request, response)
    local query = request:parse_query()
    
    local filepath = './hotfix/' .. query.version .. '/' .. query.res
    -- print('--------  ' .. filepath)
    local result = io.readfile(filepath)
    
    response.status_code = 200
    response:write_header("Content-Type","application/octet-stream")
    response:write(result)
end)

http_server.listen(table.unpack(string.split(http_server_conf.host, ':')))